#include <iostream>
#include <vector>
#include <cmath>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

const int N = 1e4 + 10;
const int INF = 2147483647;
vector<PII> edges[N];
bool st[N];
LL dist[N];
int n, m, s;
// 时间复杂度O(n^2)
void dijkstra(int s)
{
    for(int i = 0; i <= n; i++) dist[i] = INF;
    dist[s] = 0;
    for(int i = 1; i < n; i++)
    {
        int u = 0;
        for(int j = 1; j <= n; j++)
        {
            if(!st[j] && dist[j] < dist[u]) u = j;
        }
        st[u] = true;
        for(auto t : edges[u])
        {
            int v = t.first, w = t.second;
            if(dist[v] > dist[u] + w)
            {
                dist[v] = dist[u] + w;
            }
        }
    }
    for(int i = 1; i <= n; i++) cout << dist[i] << " ";
}

int main()
{
    cin >> n >> m >> s;
    for(int i = 1; i <= m; i++)
    {
        int u, v, w; cin >> u >> v >> w;
        edges[u].push_back({v, w});
    }
    dijkstra(s);
    return 0;
}